package com.vegetable.modules.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.constant.CommonConstant;
import com.vegetable.common.utils.Constant;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.Query;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.finance.FinancialTransfer;
import com.vegetable.modules.mapper.finance.FinancialTransferMapper;
import com.vegetable.modules.service.baseData.IFundAccountService;
import com.vegetable.modules.service.finance.IFinancialTransferService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 资金转账 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class FinancialTransferServiceImpl extends ServiceImpl<FinancialTransferMapper, FinancialTransfer> implements IFinancialTransferService {

    private IFundAccountService accountService;
    /**
     * 资金转账列表查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String docNo = (String)params.get("docNo");
        String startTime = (String)params.get("startTime");
        String endTime = (String)params.get("endTime");
        IPage<FinancialTransfer> page = this.page(
                new Query<FinancialTransfer>().getPage(params),
                new QueryWrapper<FinancialTransfer>()
                        .between(StringUtils.isNotBlank(startTime), "order_time", startTime, endTime)
                        .like(StringUtils.isNotBlank(docNo),"doc_no", docNo)
                        .orderByDesc("create_time")
        );

        return new PageUtils(page);
    }

    /**
     * 新增资金转账
     * @param order null
     * @return R
     * @author wrj
     * @since 2024/3/7 下午4:35
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveTransfer(FinancialTransfer order) throws Exception {
        if (order.getId() == null){
            order.setDocNo(CommonConstant.QTSK+ baseMapper.getNextCode());
            order.setOrderTime(new Date());
        }
        this.saveOrUpdate(order);
        // 更新总收款
//        FundAccount account_in = accountService.getById(order.getAccountInId());
//        if (account_in != null){
//            BigDecimal totalReceipts = account_in.getTotalReceipts();
//            totalReceipts = totalReceipts.add(order.getMoney());
//            account_in.setTotalReceipts(totalReceipts);
//            accountService.updateById(account_in);
//        }
        accountService.addReceipts(order.getAccountInId(), order.getMoney(), Constant.MoneyTYPE.TRANSFER_IN, order.getDocNo(), order.getId(), null);
        // 更新总支出
//        FundAccount account_out = accountService.getById(order.getAccountOutId());
//        if (account_out != null){
//            BigDecimal totalExpenditure = account_out.getTotalExpenditure();
//            totalExpenditure = totalExpenditure.add(order.getMoney());
//            account_out.setTotalExpenditure(totalExpenditure);
//            accountService.updateById(account_out);
//        }
        accountService.addReceipts(order.getAccountOutId(), order.getMoney(), Constant.MoneyTYPE.TRANSFER_OUT, order.getDocNo(), order.getId(), null);
        return R.status(true);
    }

    /**
     * 删除资金转账
     * @param id null
     * @return R
     * @author wrj
     * @since 2024/3/7 下午4:35
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteTransfer(Long id) throws Exception {
//        FinancialTransfer selectById = baseMapper.selectById(id);
//        if (selectById == null){
//            throw new Exception("资金转账不存在");
//        }
//
//        // 更新总收款
//        FundAccount account_in = accountService.getById(selectById.getAccountInId());
//        if (account_in != null){
//            BigDecimal totalReceipts = account_in.getTotalReceipts();
//            totalReceipts = totalReceipts.subtract(selectById.getMoney());
//            account_in.setTotalReceipts(totalReceipts);
//            accountService.updateById(account_in);
//        }
//        // 更新总支出
//        FundAccount account_out = accountService.getById(selectById.getAccountOutId());
//        if (account_out != null){
//            BigDecimal totalExpenditure = account_out.getTotalExpenditure();
//            totalExpenditure = totalExpenditure.subtract(selectById.getMoney());
//            account_out.setTotalExpenditure(totalExpenditure);
//            accountService.updateById(account_out);
//        }
//        baseMapper.deleteById(id);
        return R.fail("转账记录不可以删除");
    }
}
